package com.itheima.demo003;

import java.util.Scanner;
import java.util.StringJoiner;

public class test26_ {
    public static void main(String[] args) {
        /*键盘录入一个字符串,
        要求一:长度为小于等于九
        要求二:只能是数字
        将内容变成罗马数字
        Ⅰ-1, Ⅱ-2, Ⅲ-3, Ⅳ-4, Ⅴ-5, Ⅵ-6, Ⅶ-7, Ⅷ-8, Ⅸ-9
        注意:罗马数字没有0
        如果键盘录入的数字包含0,可以变成""(长度为0的字符串)
         */

        //1.键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        String str;
        while (true) {
            System.out.println("请输入一个字符串");
            str = sc.next();

            //2.校验字符串是否满足规则
            boolean flag = checkStr(str);
            if(flag){
                break;
            }else{
                System.out.println("当前的字符串,不符合规则.请重新输入");
                continue;
            }
        }

        //将内容变成罗马数字
        //Ⅰ-1, Ⅱ-2, Ⅲ-3, Ⅳ-4, Ⅴ-5, Ⅵ-6, Ⅶ-7, Ⅷ-8, Ⅸ-9

        //查表法:数字跟数据之间,产生对应关系
        StringJoiner sj = new StringJoiner(", ", "[", "]");
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            String s = changeLuoMa(c);
            sj.add(s);
        }
        System.out.println(sj);
    }
    /*jdk12之前的老写法:
    String str;
    switch (number){
            case '0' -> str = "";
            case '1' -> str = "Ⅰ";
            case '2' -> str = "Ⅱ";
            case '3' -> str = "Ⅲ";
            case '4' -> str = "Ⅳ";
            case '5' -> str = "Ⅴ";
            case '6' -> str = "Ⅵ";
            case '7' -> str = "Ⅶ";
            case '8' -> str = "Ⅷ";
            case '9' -> str = "Ⅸ";
            default -> str = "";
        }
     */


    //利用switch进行匹配
    public static String changeLuoMa(char number){
        String str = switch (number){
            case '0' -> "";
            case '1' -> "Ⅰ";
            case '2' -> "Ⅱ";
            case '3' -> "Ⅲ";
            case '4' -> "Ⅳ";
            case '5' -> "Ⅴ";
            case '6' -> "Ⅵ";
            case '7' -> "Ⅶ";
            case '8' -> "Ⅷ";
            case '9' -> "Ⅸ";
            default -> "";
        };

        return str;
    }


    public static boolean checkStr(String str) {
        //要求一:长度为小于等于九
        if(str.length() > 9){
            return false;
        }

        //要求二:只能是数字
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);//0~9
            if(c < '0' || c > '9'){
                return false;
            }
        }

        //只有当字符串里面,所有的字符,全部判断完毕,才能认为当前的字符串,符合规则
        return true;
    }
}
